package api

import (
	"context"
	"github.com/kataras/iris/v12/mvc"
	"micro-grpc-server/app/common"
	"micro-grpc-server/app/http/controllers"
	pb "micro-grpc-server/services/user/proto"
)

type DefaultController struct {
	controllers.Controller
	UserClient pb.UserService
}

func (c *DefaultController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("GET", "/user/debug/{id:int64}", "Debug")
}

// Debug
// @Router /user/debug/{id} [get]
// @Summary 调试登录
// @Description 调试登录
// @Tags User
// @Produce json
// @Param id path int true "用户id"
// @Success 200
func (c *DefaultController) Debug(id int64, ctx context.Context) {
	if !common.IsDebug() {
		c.BadRequest("访问失败")
		return
	}
	_, err := c.UserClient.UserProfile(ctx, &pb.UserIdRequest{UserId: id})
	if err != nil {
		c.BadRequest(c.FriendlyRpcError(err))
		return
	}

	redirectUri := c.Ctx.URLParamDefault("redirect_uri", "")
	if len(redirectUri) > 0 {
		c.Session.Set("auth_admin_user_id", id)
		c.Ctx.Redirect(redirectUri)
		return
	}

	c.JsonSuc("success")
}
